package com.youbug.mall.coupon_sms.feign.impl;

import com.youbug.common.to.SeckillSessionWithSkusTo;
import com.youbug.common.to.SeckillSkuTo;
import com.youbug.common.vo.R;
import com.youbug.mall.coupon_sms.entity.SmsSeckillSession;
import com.youbug.mall.coupon_sms.service.ISmsSeckillSessionService;
import com.youbug.mall.seckill.feign.ISeckillCouponFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SeckillCouponFeignServiceImpl implements ISeckillCouponFeignService {
    @Autowired
    private ISmsSeckillSessionService seckillSessionService;

    @Override
    public R<List<SeckillSessionWithSkusTo>> getLates3DaySession() {

        List<SmsSeckillSession> seckillSessions = seckillSessionService.getLates3DaySession();
        if(!CollectionUtils.isEmpty(seckillSessions)){

            List<SeckillSessionWithSkusTo> withSkusTos = seckillSessions.stream().map(e -> {
                SeckillSessionWithSkusTo skusTo = new SeckillSessionWithSkusTo();
                BeanUtils.copyProperties(e, skusTo);

                List<SeckillSkuTo> skuToList = e.getRelationSkusTemp().stream().map(t -> {
                    SeckillSkuTo seckillSkuTo = new SeckillSkuTo();
                    BeanUtils.copyProperties(t, seckillSkuTo);
                    return seckillSkuTo;
                }).collect(Collectors.toList());

                skusTo.setRelationSkus(skuToList);
                return skusTo;
            }).collect(Collectors.toList());

            return R.ok(withSkusTos);
        }
        return R.fail();
    }
}
